// Author: Domien Nowicki

#include "SingleGameWidget.h"
#include "HighscoreDialog.h"
#include "PaintPaletteWidget.h"
#include "ClockWidget.h"
#include <QHboxLayout>
#include <QInputDialog>
#include <QMessageBox>
#include <sstream>
#include <iomanip>
using namespace std;

SingleGameWidget::SingleGameWidget(QWidget *parent, const Map& newMap, const Properties& properties, Highscore& highscore): QWidget(parent), SingleGame(newMap, properties, highscore)
{
	// Setup single game ui
    gridLayout = new QGridLayout(this);
    gridLayout->setSpacing(6);
    gridLayout->setMargin(9);

	topHboxLayout = new QHBoxLayout(this);

    textEdit = new QTextEdit(this);
    textEdit->setMaximumSize(QSize(16777215, 80));
    textEdit->setCursor(QCursor(static_cast<Qt::CursorShape>(0)));
    textEdit->setReadOnly(true);

	topHboxLayout->addWidget(textEdit);

	gridLayout->addLayout(topHboxLayout, 0, 0, 1, 1);

	hboxLayout = new QHBoxLayout(this);
    hboxLayout->setSpacing(6);
    hboxLayout->setMargin(0);

    gridLayout->addLayout(hboxLayout, 1, 0, 1, 1);

	labelStepCount = new QLabel(this);

    gridLayout->addWidget(labelStepCount, 2, 0, 1, 1);
}


SingleGameWidget::~SingleGameWidget()
{
}

// Get name to enter it in the highscores
bool SingleGameWidget::GetName(std::string *name)
{
    bool ok;

    QString text;
	
	do
	{
		text = QInputDialog::getText(this, tr("Enter your name"), tr("You have made it to the highscores!\nEnter your name:"), QLineEdit::Normal, "", &ok);
		// Make sure the user did not click ok with an empty name
	}
    while(ok && text.isEmpty());

	if (ok)
	{
		*name = text.toStdString();
	}

	return ok;
}

// Show the highscores
void SingleGameWidget::ShowHighscore(const Highscore& highscore)
{
	HighscoreDialog* dialog = new HighscoreDialog(this, highscore);

	dialog->show();
}

// Get the amount of colors to use when coloring the map, which must be between the min and max
int SingleGameWidget::GetNumber(int min, int max)
{
     bool ok;

	 int i = QInputDialog::getInteger(this, tr("Color map"), tr("Select the amount of colors to use:"), min, min, max, 1, &ok);

     if (ok)
	 {
		 // Pressed ok
		 return i;
	 }
	 else
	 {
		 // Pressed cancel
		 return RESTRICTIONABORT;
	 }
}

// Show the move we just did
SingleGame::ColorMapMethod SingleGameWidget::ShowMove(const Move& move)
{
	SingleGame::ColorMapMethod result = COLORABORT;

	

	std::string message = "Completing map: "+move.ToString()+"\nSelect Ok to complete the whole map at once, or select Retry to do a single step.";

	// Ask what to do
	int reply = QMessageBox::information(this, tr("Coloring map"), message.c_str(), QMessageBox::Ok | QMessageBox::Abort | QMessageBox::Retry);

	switch(reply)
	{
		case QMessageBox::Ok:
			result = COLORALL;
		break;

		case QMessageBox::Abort:
			result = COLORABORT;
		break;

		case QMessageBox::Retry:
			result = COLORCONTINUE;
		break;
	}

	return result;
}

void SingleGameWidget::Started()
{
	emit started(this);
}

void SingleGameWidget::Stopped()
{
	emit stopped();
}

// Create new map
Map* SingleGameWidget::NewMap(const Map& map)
{
	MapWidget *mapWidget = new MapWidget(this, map);

	// Add widget
    hboxLayout->insertWidget(-1, mapWidget);

	return mapWidget;
}

// Create new clock
Clock* SingleGameWidget::NewClock(Clock::ClockType type)
{
	ClockWidget *clockWidget = new ClockWidget(this, type);

	// Add widget
	topHboxLayout->addWidget(clockWidget);

	return clockWidget;
}

// Create new human player
Player* SingleGameWidget::NewHumanPlayer(Map* map)
{
	HumanPlayerWidget *playerWidget = new HumanPlayerWidget(this, this, map, GetProperties());

	// Add widget
	hboxLayout->insertWidget(0, playerWidget);

	return playerWidget;
}

// Show the amount of steps the player has done
void SingleGameWidget::ShowPlayerStepCount(int steps)
{
	ostringstream s;


	s << "Player step count: " << steps;	
	labelStepCount->setText(s.str().c_str());
}

// Show output to screen
void SingleGameWidget::Output(std::string message)
{
	textEdit->append(QString(message.c_str()));
}



